<?php
	/**
	 * 栏目选择
	 * @param string $file 栏目缓存文件名
	 * @param intval/array $catid 别选中的ID，多选是可以是数组
	 * @param string $str 属性
	 * @param string $default_option 默认选项
	 * @param intval $modelid 按所属模型筛选
	 * @param intval $type 栏目类型
	 * @param intval $onlysub 只可选择子栏目
	 * @param intval $siteid 如果设置了siteid 那么则按照siteid取
	 */
 function select_category($file = '',$catid = 0, $str = '', $default_option = '', $modelid = 0, $type = -1, $onlysub = 0,$siteid = 0,$is_push = 0) {
		$tree = pc_base::load_sys_class('tree');
		if(!$siteid) $siteid = param::get_cookie('siteid');
		if (!$file) {
			$file = 'category_content_'.$siteid;
		}
		$result = getcache($file,'ushop');
		$string = '<select '.$str.'>';
		if($default_option) $string .= "<option value='0'>$default_option</option>";
		//加载权限表模型 ,获取会员组ID值,以备下面投入判断用
		if($is_push=='1'){
			$priv = pc_base::load_model('category_priv_model');
			$user_groupid = param::get_cookie('_groupid') ? param::get_cookie('_groupid') : 8;
		}
		if (is_array($result)) {
			foreach($result as $r) {
 				//检查当前会员组，在该栏目处是否允许投稿？
				if($is_push=='1' and $r['child']=='0'){
					$sql = array('catid'=>$r['catid'],'roleid'=>$user_groupid,'action'=>'add');
					$array = $priv->get_one($sql);
					if(!$array){
						continue;	
					}
				}
				if($siteid != $r['siteid'] || ($type >= 0 && $r['type'] != $type)) continue;
				$r['selected'] = '';
				if(is_array($catid)) {
					$r['selected'] = in_array($r['catid'], $catid) ? 'selected' : '';
				} elseif(is_numeric($catid)) {
					$r['selected'] = $catid==$r['catid'] ? 'selected' : '';
				}
				$r['html_disabled'] = "0";
				if (!empty($onlysub) && $r['child'] != 0) {
					$r['html_disabled'] = "1";
				}
				$categorys[$r['catid']] = $r;
				if($modelid && $r['modelid']!= $modelid ) unset($categorys[$r['catid']]);
			}
		}
		$str  = "<option value='\$catid' \$selected>\$spacer \$catname</option>;";
		$str2 = "<optgroup label='\$spacer \$catname'></optgroup>";

		$tree->init($categorys);
		$string .= $tree->get_tree_category(0, $str, $str2);
			
		$string .= '</select>';
		return $string;
	}

/**
 * 当前路径 
 * 返回指定栏目路径层级
 * @param $catid 栏目id
 * @param $symbol 栏目间隔符
 */
function shop_catpos($catid, $symbol=' > '){
	$category_arr = array();
	$siteids = getcache('category_content','ushop');
	$siteid = $siteids[$catid];
	$category_arr = getcache('category_content_'.$siteid,'ushop');
	if(!isset($category_arr[$catid])) return '';
	$pos = '';
	$siteurl = siteurl($category_arr[$catid]['siteid']);
	$arrparentid = array_filter(explode(',', $category_arr[$catid]['arrparentid'].','.$catid));
	foreach($arrparentid as $catid) {
		$url = $category_arr[$catid]['url'];
		if(strpos($url, '://') === false) $url = $siteurl.$url;
		$pos .= '<a href="'.$url.'">'.$category_arr[$catid]['catname'].'</a>'.$symbol;
	}
	return $pos;
}
/**
 * 当前sessid 
 	生成session
 * 
 * 
 */
 function create_sess_id($len=32) 
      { 
           // 校验提交的长度是否合法 
              if( !is_numeric($len) || ($len>32) || ($len<16)) { return; } 
             // 获取当前时间的微秒 
             list($u, $s) = explode(' ', microtime()); 
            $time = (float)$u + (float)$s; 
              // 产生一个随机数 
             $rand_num = rand(100000, 999999); 
             $rand_num = rand($rand_num, $time); 
             mt_srand($rand_num); 
            $rand_num = mt_rand(); 
           // 产生SessionID 
            $sess_id = md5( md5($time). md5($rand_num) ); 
          // 截取指定需要长度的SessionID 
            $sess_id = substr($sess_id, 0, $len); 
           return $sess_id; 
} 



function csv_to_array($csv)
{
	$len = strlen($csv);


	$table = array();
	$cur_row = array();
	$cur_val = "";
	$state = "first item";


	for ($i = 0; $i < $len; $i++)
	{
		//sleep(1000);
		$ch = substr($csv,$i,1);
		if ($state == "first item")
		{
			if ($ch == '"') $state = "we're quoted hea";
			elseif ($ch == ",") //empty
			{
				$cur_row[] = ""; //done with first one
				$cur_val = "";
				$state = "first item";
			}
			elseif ($ch == "\n")
			{
				$cur_row[] = $cur_val;
				$table[] = $cur_row;
				$cur_row = array();
				$cur_val = "";
				$state = "first item";
			}
			elseif ($ch == "\r") $state = "wait for a line feed, if so close out row!";
			else
			{
				$cur_val .= $ch;
				$state = "gather not quote";
			}
			
		}

		elseif ($state == "we're quoted hea")
		{
			if ($ch == '"') $state = "potential end quote found";
			else $cur_val .= $ch;
		}
		elseif ($state == "potential end quote found")
		{
			if ($ch == '"')
			{
				$cur_val .= '"';
				$state = "we're quoted hea";
			}
			elseif ($ch == ',')
			{
				$cur_row[] = $cur_val;
				$cur_val = "";
				$state = "first item";
			} elseif ($ch == "\n") {
				$cur_row[] = $cur_val;
				$table[] = $cur_row;
				$cur_row = array();
				$cur_val = "";
				$state = "first item";
			} elseif ($ch == "\r") {
				$state = "wait for aline feed, if so close out row!";
			} else {
				$cur_val .= $ch;
				$state = "we're quoted hea";
			}

		} elseif ($state == "wait for a line feed, if so close out row!") {
			if ($ch == "\n") {
				$cur_row[] = $cur_val;
				$cur_val = "";
				$table[] = $cur_row;
				$cur_row = array();
				$state = "first item";

			} else {
				$cur_row[] = $cur_val;
				$table[] = $cur_row;
				$cur_row = array();
				$cur_val = $ch;
				$state = "gather not quote";
			}	
		} elseif ($state == "gather not quote") {
			if ($ch == ",") {
				$cur_row[] = $cur_val;
				$cur_val = "";
				$state = "first item";
				
			} elseif ($ch == "\n") {
				$cur_row[] = $cur_val;
				$table[] = $cur_row;
				$cur_row = array();
				$cur_val = "";
				$state = "first item";
			}
			elseif ($ch == "\r") $state = "wait for a line feed, if so close out row!";
			else $cur_val .= $ch;
		}

	}
	return $table;
}
function create_sn(){
	mt_srand((double )microtime() * 1000000 );
	return date("YmdHis" ).str_pad( mt_rand( 1, 99999 ), 5, "0", STR_PAD_LEFT );
}
function mk_pay_btn($data,$attr='class="payment-show"',$ishow='1') {
	$pay_type = '';
	if(is_array($data)){
		foreach ($data as $v) {
			$pay_type .= '<label '.$attr.'>';
			$pay_type .='<input name="pay_type" type="radio" value="'.$v['pay_id'].'"> <em>'.$v['name'].'</em>';
			$pay_type .=$ishow ? '<span class="payment-desc">'.$v['pay_desc'].'</span>' :'';
			$pay_type .= '</label>';
		}
	}
	return $pay_type;
}

function order_by_title0($a,$b) {
	return strcasecmp($a['title'],$b['title']);
}
function order_by_title1($a,$b) {
	return -(strcasecmp($a['title'],$b['title']));
}
function order_by_member0($a,$b) {
	return $a['member'] > $b['member'];
}
function order_by_member1($a,$b) {
	return $a['member'] < $b['member'];
}
function order_by_bonus($a,$b) {
	return $a['min_amount'] < $b['min_amount'];
}

	//订单状态
function order_status($num) {
	switch($num) {
		case 0:
			$order_status = "未确认";
		break;
		case 2:
			$order_status = "未确认";
		break;
		case 3:
			$order_status = "无效";
		break;
		case 4:
			$order_status = "<span style='color:red'>退贷</span>";
		break;
		default:
			$order_status = "已确认";
		break;
	}
	return $order_status;
}
//发贷状态
function shipping_status($num) {
	switch($num) {
		case 0:
			$shipping_status = "未发贷";
		break;
		case 1:
			$shipping_status = "已发贷";
		break;
		case 2:
			$shipping_status = "收货确认";
		break;
		case 4:
			$shipping_status = "已发货(部分商品)";
		break;
		default:
			$shipping_status = "配货中";
		break;
	}
	return $shipping_status;
}
//支付状态
function pay_status($num) {
	switch($num) {
		case 0:
			$pay_status = "未付款";
		break;
		case 1:
			$pay_status = "付款中";
		break;
		case 2:
			$pay_status = "已付款";
		break;
	}
	return $pay_status;
}

//计算多维数组中某个字段的和
function arraysum($array,$file) {
	foreach($array as $key => $value) {
		$sum = (int)$sum + (int)$value[$file];
	}
	return $sum;
}


function array_icount_values($arr,$lower=true) {
	$arr2=array();
	if(!is_array($arr['0'])){$arr=array($arr);}
	foreach($arr as $k=> $v){
		if(!isset($arr2[$v['title'].'&&'.$v['price']])) {
			$arr2[$v['title'].'&&'.$v['price']] = 1;
		}else{ 
			$arr2[$v['title'].'&&'.$v['price']] ++ ;
		}
	}
	return $arr2;
} 
?>